package model

import (
	"encoding/json"
	"fmt"
	"github.com/stretchr/testify/assert"
	"testing"

	"go.mongodb.org/mongo-driver/bson"
)

type reverseSuit struct {
	value    string
	expected *Environment
}

func TestEnvironment_JSON(t *testing.T) {
	TestJsonSuits(t, []Suit{
		{&Environment{"key": "value"}, "{\"key\":\"value\"}"},
		{&Environment{"key": "true"}, "{\"key\":\"true\"}"},
		{&Environment{"key": "1"}, "{\"key\":\"1\"}"},
	}, func(bytes []byte) (interface{}, error) {
		var result Environment
		err := json.Unmarshal(bytes, &result)
		return &result, err
	})

}

func TestEnvironment_JSONUnmarshall(t *testing.T) {
	reverse := []reverseSuit{
		{"{\"key\": \"value\"}", &Environment{"key": "value"}},
		{"{\"key\": 1}", &Environment{"key": "1"}},
		{"{\"key\": null}", &Environment{}},
		{"{\"key\": true}", &Environment{"key": "true"}},
	}

	for i, current := range reverse {
		t.Run(fmt.Sprintf("Scenario-%d", i), func(t *testing.T) {
			var actual Environment
			if err := json.Unmarshal([]byte(current.value), &actual); err != nil {
				t.Fatalf("failed to unmarshall '%s': %s", current.value, err)
			}

			assert.Equal(t, current.expected, &actual)
		})
	}
}

func TestEnvironment_BSON(t *testing.T) {
	TestBsonSuits(
		t,
		[]Suit{
			{&Environment{"key": "value"}, "{\"key\": \"value\"}"},
			{&Environment{"key": "true"}, "{\"key\": \"true\"}"},
			{&Environment{"key": "1"}, "{\"key\": \"1\"}"},
		}, func(raw bson.RawValue) (interface{}, error) {
			var result Environment
			err := raw.Unmarshal(&result)
			return &result, err
		})
}
